//
//  ViewController.m
//  Gestures
//
//  Created by Mikeliang on 10/9/14.
//  Copyright (c) 2014 ExStudio. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *labelMain;

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self addGestures];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void) addGestures {
    // Tap
    _labelMain.userInteractionEnabled = TRUE;
    
    // 各种点击事件
    int maxTaps = 4, maxTouches = 4;
    for (int i = 1; i <= maxTaps; i++) {
        for (int j = 1; j <= maxTouches; j++) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
            tap.numberOfTapsRequired = i; // 点击几次
            tap.numberOfTouchesRequired = j; // 几只手指
            [_labelMain addGestureRecognizer:tap];
        }
    }
    
    // 滑动，上下左右，若干手指
    for (int i = 1; i <= maxTouches; i++) {
        for (int j = UISwipeGestureRecognizerDirectionRight; j <= UISwipeGestureRecognizerDirectionDown; j=j*2) {
            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
            swipe.numberOfTouchesRequired = i; // 用几根手指
            swipe.direction = j; // 滑动方向
            [_labelMain addGestureRecognizer:swipe];
        }
    }
    
    // 缩放
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self  action:@selector(handlePinch:)];
    [_labelMain addGestureRecognizer:pinch];
    
    // Rotation
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self  action:@selector(handleRotation:)];
    [_labelMain addGestureRecognizer:rotation];
    
    // 拖动
    /*UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self  action:@selector(handlePan:)];
     pan.minimumNumberOfTouches = 1;
     pan.maximumNumberOfTouches = 1;
     [_labelMain addGestureRecognizer:pan];*/
    
    UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longpress.numberOfTouchesRequired = 1;
    longpress.numberOfTapsRequired = 0;
    longpress.minimumPressDuration = 2;
    longpress.allowableMovement = 10;
    [_labelMain addGestureRecognizer:longpress];
}

- (void) handleTap:(UITapGestureRecognizer*)sender {
    _labelMain.text = [[NSString alloc] initWithFormat:@"Tap %lu times with %lu fingers",
                       (unsigned long)sender.numberOfTapsRequired,
                       (unsigned long)sender.numberOfTouchesRequired];
}
- (void) handleSwipe:(UISwipeGestureRecognizer*)sender {
    NSDictionary *dict = @{@1: @"Right", @2:@"Left", @4:@"Up", @8:@"Down"};
    NSNumber *key = [NSNumber numberWithInt:sender.direction];
    _labelMain.text = [[NSString alloc] initWithFormat:@"Swipe %@ with %lu fingers",
                       [dict objectForKey: key], // Dictionary的下标必须要是 NSNumber
                       (unsigned long)sender.numberOfTouchesRequired];
}
- (void) handlePinch:(UIPinchGestureRecognizer*)sender {
    _labelMain.text = [[NSString alloc] initWithFormat:@"Pinch \n scale=%f \n velocity=%f",
                       sender.scale, sender.velocity];
}
- (void) handlePan:(UIPanGestureRecognizer*)sender {
    _labelMain.text = @"pan the label";
}
- (void) handleRotation:(UIRotationGestureRecognizer*)sender {
    _labelMain.text = [[NSString alloc] initWithFormat:@"Rotate \n rotation=%f \n velocity=%f",
                       sender.rotation, sender.velocity];
}
- (void) handleLongPress:(UIGestureRecognizer*)sender {
    _labelMain.text = @"long press the label";
}



@end
